package com.wash.shoes.controller.pc;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.wash.shoes.domain.dto.RegionTransactionDetailDto;
import com.wash.shoes.domain.vo.RegionTransactionDetailVo;
import com.wash.shoes.entity.RegionDistributorPo;
import com.wash.shoes.entity.RegionTransactionDetailPo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.service.RegionDistributorService;
import com.wash.shoes.service.RegionTransactionDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author ljg
 * @since 2024-10-11
 */
@Log4j2
@Api(tags = {"PC-分销-收支统计"})
@RestController
@RequestMapping("/regionTransactionDetail")
public class RegionTransactionDetailController {

    @Resource
    RegionTransactionDetailService regionTransactionDetailService;

    @Resource
    RegionDistributorService regionDistributorService;

    @PostMapping("/list")
    @ApiOperation("提现申请")
    public R<Page<RegionTransactionDetailVo>> pageList(@RequestBody RegionTransactionDetailDto regionTransactionDetailPo) {
        Page<RegionTransactionDetailVo> page = regionTransactionDetailService.pageList(regionTransactionDetailPo);
        return R.ok(page);
    }

    @PostMapping("/userWithdrawalRequest")
    @ApiOperation("管理员工,操作用户提现 1成功 2失败")
    public R<String> userWithdrawalRequest(String id, String remark, Integer status) {

        RegionTransactionDetailPo po = regionTransactionDetailService.getById(id);
        if (po.getStatus() != 0){
            throw new WashException("非如帐中的订单不能提现");
        }
        // 修改用户的金额
        RegionDistributorPo regionDistributorPo = regionDistributorService.getByUserId(po.getRecommendId());
        if (regionDistributorPo.getStatus() == 0){
            throw new WashException("禁用状态不能提现");
        }
        // 提现失败
        if (status == 2){
            po.setStatus(status);
            po.setRemark(remark);
            regionTransactionDetailService.updateById(po);
            return R.ok();
        }
        // 提现成功
        if (status == 1){
            po.setStatus(status);
            po.setRemark(remark);
            regionTransactionDetailService.updateById(po);
            regionDistributorPo.setPromotionProfit(regionDistributorPo.getPromotionProfit() - po.getPromotionProfit());
            regionDistributorService.updateById(regionDistributorPo);
            return R.ok();
        }

        throw new WashException("系统错误");
    }

}

